package com.itdaan.portal.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
 * @author www.itdaan.com
 */
public class TimeUtil {
	
	public static final Log logger = LogFactory.getLog(TimeUtil.class);

	public static final String FULL_PATTERN = "yyyy-MM-dd HH:mm:ss";
	public static final String SIMPLE_PATTERN = "yyyy-MM-dd";
	
	public static final SimpleDateFormat sdf = new SimpleDateFormat();
	
	/**
	 * 字符串时间转化成Date时间
	 * @param source 输入的String时间
	 * @param pattern 格式
	 * @return Date时间
	 * @throws ParseException
	 */
	public static Date string2Time(String source,String pattern) throws ParseException{
		sdf.applyPattern(pattern);
		return sdf.parse(source);
	}
	
	/**
	 * 按照 FULL_PATTERN 或 SIMPLE_PATTERN 格式解析时间字符串
	 * 	FULL_PATTERN : "yyyy-MM-dd HH:mm:ss"
	 * 	SIMPLE_PATTERN : "yyyy-MM-dd"
	 * @param source 输入的string时间
	 * @param useFullPattern 是否使用FULL_PATTERN
	 * @return Date时间
	 */
	public static Date string2Time(String source,boolean useFullPattern){
		String pattern = useFullPattern ? FULL_PATTERN : SIMPLE_PATTERN;
		try {
			return string2Time(source, pattern);
		} catch (ParseException e) {
			logger.error("Parse string time error using pattern: " + pattern);
		}
		return null;
	}
	
	/**
	 * 根据输入的时间，获取当天的开始时间
	 * 比如输入2010-04-19，
	 * 则函数输出的Date的值是2010-04-19 00:00:00。
	 * @param source 必须是yyyy-MM-dd或者yyyy-MM-dd HH:mm:ss格式表示的时间
	 * @return Date
	 * @throws ParseException
	 */
	public static Date string2BeginTime(String source) throws ParseException{
		if(source==null || source.length()< 10)
			return null;
		source = source.substring(0, 10);
		String input = source + " 00:00:00";
		return string2Time(input, FULL_PATTERN);
	}
	
	/**
	 * 根据输入的时间，获取当天的结束时间
	 * 比如输入2010-04-19，
	 * 则函数输出的Date的值是2010-04-19 23:59:59。
	 * @param source 必须是yyyy-MM-dd或者yyyy-MM-dd HH:mm:ss格式表示的时间
	 * @return Date
	 * @throws ParseException
	 */	
	public static Date string2EndTime(String source) throws ParseException{
		if(source==null || source.length()< 10)
			return null;
		source = source.substring(0, 10);		
		String input = source + " 23:59:59";
		return string2Time(input, FULL_PATTERN);
	}
	
	/**
	 * Date时间转换成String时间，按照pattern格式
	 * @param time
	 * @param pattern 时间格式
	 * @return String
	 */
	public static String time2String(Date time,String pattern){
		sdf.applyPattern(pattern);
		return sdf.format(time);
	}
	
	/**
	 * Date时间转换成完整时间的string输出
	 * @param time
	 * @return yyyy-MM-dd HH:mm:ss 格式的string时间
	 */
	public static String time2FullString(Date time){
		return time2String(time, FULL_PATTERN);
	}
	
	/**
	 * Date时间转换成简单时间的string输出
	 * @param time
	 * @return yyyy-MM-dd 格式的string时间
	 */
	public static String time2SimpleString(Date time){
		return time2String(time, SIMPLE_PATTERN);
	}
	
	public static final int UNIT_sec = Calendar.SECOND; 
	public static final int UNIT_min = Calendar.MINUTE; 
	public static final int UNIT_hour = Calendar.HOUR; 
	public static final int UNIT_day = Calendar.DATE; 
	public static final int UNIT_month = Calendar.MONTH; 
	public static final int UNIT_year = Calendar.YEAR; 
	
	/**
	 * 时间加减函数,可按年/月/日/时/分/秒单位，对时间进行加减操作
	 * @param unit 单位
	 * 	 There are public static declaration in this class:
	 * 	 UNIT_sec/UNIT_min/UNIT_hour/UNIT_day/UNIT_month/UNIT_year
	 * @param amount 数量
	 * @param orgDate 输入时间
	 * @return Date 加减后的时间
	 */
	public static Date timeAdd(int unit,int amount,Date orgDate){
		Calendar cl = Calendar.getInstance();
		cl.setTime(orgDate);
		cl.add(unit, amount);
		
		return cl.getTime();
	}
	
	/**
	 * 计算时间差(dB-dA)
	 * @param dA
	 * @param dB
	 * @return 相差的秒数
	 */
	public static int timeDiff(Date dA,Date dB){
		return (int)((dB.getTime() - dA.getTime())/1000);
	}
	
	/**
	 * 时区时间字符串转化成当地时间
	 * @param fmtTime 
	 * 		yyyy-MM-ddTHH:mm:ss+08:00
	 * 		yyyy-MM-ddTHH:mm:ss+00:00
	 * @return Date
	 */
	public static Date fmtTimeToLocalDate(String fmtTime){
		
		if(fmtTime.length() != 25)
			return null;
		
		String s1 = fmtTime.substring(0, 10);
		String s2 = fmtTime.substring(11, 19);
		String s3 = fmtTime.substring(20, 22);
		
		Date tempDate = TimeUtil.string2Time(s1+" "+s2, true);
		
		int offsetSrc = Integer.parseInt(s3);
		int offsetLocal = Calendar.getInstance().getTimeZone().getRawOffset()/3600000;
		
		int diff = offsetLocal-offsetSrc;
		return TimeUtil.timeAdd(TimeUtil.UNIT_hour, diff, tempDate);
	}	
	
	public static void main(String[] args) {
//		String source = "2010-04-19 10:15:20";
//		try {
//			System.out.println(TimeUtil.string2Time(source, TimeUtil.FULL_PATTERN));
//			System.out.println(TimeUtil.string2Time(source, true));
//			
//			source = "2010-04-19";
//			System.out.println(TimeUtil.string2Time(source, false));
//			System.out.println(TimeUtil.string2BeginTime(source));
//			System.out.println(TimeUtil.string2EndTime(source));
//			
//		} catch (ParseException e) {
//			e.printStackTrace();
//		}
		
		Date now = new Date();
		System.out.println(TimeUtil.time2FullString(now));
		
//		System.out.println("-------------------------");
//		System.out.println(TimeUtil.time2String(now, TimeUtil.FULL_PATTERN));
//		System.out.println(TimeUtil.time2FullString(now));
//		System.out.println(TimeUtil.time2SimpleString(now));
		
		System.out.println("---------------------------");
		
//		System.out.println(TimeUtil.timeAdd(TimeUtil.UNIT_sec, 1, now));
//		System.out.println(TimeUtil.timeAdd(TimeUtil.UNIT_min, 1, now));
//		System.out.println(TimeUtil.timeAdd(TimeUtil.UNIT_hour, 1, now));
//		System.out.println(TimeUtil.timeAdd(TimeUtil.UNIT_month, 1, now));
//		System.out.println(TimeUtil.timeAdd(TimeUtil.UNIT_year, 1, now));
		
		System.out.println(TimeUtil.fmtTimeToLocalDate("2010-05-06T02:50:00+00:00"));
	}
	
	
}
